<?php

$con = mysqli_connect("localhost","root","root","picknroll_db");

if (mysqli_connect_errno())
{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$filename = "fullplayerstats.data";
$file = file_get_contents("out/" . $filename);
$lines = explode("\n", $file);

$num_lines = count($lines);

$sql = "INSERT INTO player\n\t(".
		"rnk_yhoo, rnk_espn, rnk_razz, val_zsea, val_zprj, name, team, pos, \n\t".
		"gms, min, prj_fgm, prj_fga, prj_fg, prj_ftm, prj_fta, prj_ft, prj_3ptm, \n\t".
		"prj_pts, prj_reb, prj_ast, prj_stl, prj_blk, prj_to, sea_fgm, sea_fga, \n\t".
		"sea_fg, sea_ftm, sea_fta, sea_ft, sea_3ptm, sea_pts, sea_reb, sea_ast, \n\t".
		"sea_stl, sea_blk, sea_to, avg_3ptm, avg_pts, avg_reb, avg_ast, avg_stl, \n\t".
		"avg_blk, avg_to, zsea_fg, zsea_ft, zsea_3ptm, zsea_pts, zsea_reb, zsea_ast, \n\t".
		"zsea_stl, zsea_blk, zsea_to, zprj_fg, zprj_ft, zprj_3ptm, zprj_pts, zprj_reb, \n\t".
		"zprj_ast, zprj_stl, zprj_blk, zprj_to".
		")\n".
		"VALUES";

for ($i = 1; $i < $num_lines; $i++)
{
	$field = explode("|", $lines[$i]);

	$rnk_yhoo = $field[0];	$rnk_espn = $field[1]; 	$rnk_razz = $field[2];
	$val_zsea = $field[3];	$val_zprj = $field[4];

	$name = $field[5];		$team = $field[6];		$pos = $field[7];
	$gms = $field[8];		$min = $field[9];

	$prj_fgm = $field[10];	$prj_fga = $field[11];
	$prj_fg = $field[12];	$prj_ftm = $field[13];
	$prj_fta = $field[14];	$prj_ft = $field[15];
	$prj_3ptm = $field[16];	$prj_pts = $field[17];
	$prj_reb = $field[18];	$prj_ast = $field[19];
	$prj_stl = $field[20];	$prj_blk = $field[21];
	$prj_to = $field[22];

	$sea_fgm = $field[23];	$sea_fga = $field[24];
	$sea_fg = $field[25];	$sea_ftm = $field[26];
	$sea_fta = $field[27];	$sea_ft = $field[28];
	$sea_3ptm = $field[29];	$sea_pts = $field[30];
	$sea_reb = $field[31];	$sea_ast = $field[32];
	$sea_stl = $field[33];	$sea_blk = $field[34];
	$sea_to = $field[35];

	$avg_3ptm = $field[36];	$avg_pts = $field[37];
	$avg_reb = $field[38];	$avg_ast = $field[39];
	$avg_stl = $field[40];	$avg_blk = $field[41];
	$avg_to = $field[42];

	$zsea_fg = $field[43];		$zsea_ft = $field[44];
	$zsea_3ptm = $field[45];	$zsea_pts = $field[46];
	$zsea_reb = $field[47];		$zsea_ast = $field[48];
	$zsea_stl = $field[49];		$zsea_blk = $field[50];
	$zsea_to = $field[51];

	$zprj_fg = $field[52];		$zprj_ft = $field[53];
	$zprj_3ptm = $field[54];	$zprj_pts = $field[55];
	$zprj_reb = $field[56];		$zprj_ast = $field[57];
	$zprj_stl = $field[58];		$zprj_blk = $field[59];
	$zprj_to = $field[60];

	if ($i > 1)
		$sql .= ",";

	$sql .= "\n\t(".
		"$rnk_yhoo, $rnk_espn, $rnk_razz, $val_zsea, $val_zprj, ".
		"\"".mysqli_real_escape_string($con, $name)."\", ".
		"\"".mysqli_real_escape_string($con, $team)."\", ".
		"\"".mysqli_real_escape_string($con, $pos)."\", \n\t\t".
		"$gms, $min, $prj_fgm, $prj_fga, $prj_fg, $prj_ftm, $prj_fta, $prj_ft, $prj_3ptm, \n\t\t".
		"$prj_pts, $prj_reb, $prj_ast, $prj_stl, $prj_blk, $prj_to, $sea_fgm, $sea_fga, \n\t\t".
		"$sea_fg, $sea_ftm, $sea_fta, $sea_ft, $sea_3ptm, $sea_pts, $sea_reb, $sea_ast, \n\t\t".
		"$sea_stl, $sea_blk, $sea_to, $avg_3ptm, $avg_pts, $avg_reb, $avg_ast, $avg_stl, \n\t\t".
		"$avg_blk, $avg_to, $zsea_fg, $zsea_ft, $zsea_3ptm, $zsea_pts, $zsea_reb, $zsea_ast, \n\t\t".
		"$zsea_stl, $zsea_blk, $zsea_to, $zprj_fg, $zprj_ft, $zprj_3ptm, $zprj_pts, $zprj_reb, \n\t\t".
		"$zprj_ast, $zprj_stl, $zprj_blk, $zprj_to".
		")";
}
$sql .= ";";
if (file_put_contents("players.sql", $sql) !== false)
{	
	echo "<h3>Script created. See players.sql</h3>";
	echo "<iframe src='players.sql' width='100%' height='80%'>";
}
else
{
	echo "<h3>Error processing raw data.</h3>";
}
?>